using System.Drawing;
using GTA;
using GTA.UI;
using NativeUI;

namespace CustomPeds.Menus;

public class VehicleSexMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.vhcs_Menu, new Point(0, -107));
    private static bool inited;

    public VehicleSexMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            Build();
            inited = true;
        }
    }

    public static void Open()
    {
        if (!inited) return;
        Build();
        menu.Visible = true;
        menu.RefreshIndex();
    }

    private static void Build()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());

        var oral = new UIMenuItem(TextSetting.vhcs_MenuItem_Oral);
        var sex = new UIMenuItem(TextSetting.vhcs_MenuItem_Sex);

        oral.Activated += (m, i) => { StartVehicleSex(VehicleSexScript.SexType.SexOralSit); };
        sex.Activated += (m, i) => { StartVehicleSex(VehicleSexScript.SexType.SexSit); };

        menu.AddItem(oral);
        menu.AddItem(sex);
        menu.RefreshIndex();
    }

    private static void StartVehicleSex(VehicleSexScript.SexType type)
    {
        try
        {
            if (Main.P1.Character == null || !Main.P1.Character.IsAlive)
            {
                Notification.PostTicker("玩家不可用，无法开始互动", true, false);
                return;
            }
            if (Main.P2.Character == null || !Main.P2.Character.IsAlive)
            {
                Notification.PostTicker("女友不可用，无法开始互动", true, false);
                return;
            }
            if (!Main.P1.Character.IsInVehicle() || !Main.P2.Character.IsInVehicle())
            {
                Notification.PostTicker("需在车内才能进行车辆互动", true, false);
                return;
            }
            Main.m_Status = Main.MaleStatus.Vehicle;
            VehicleSexScript.InitSexPlay(type);
            Main.OnMainMenuHidden();
            menu.Visible = false;
        }
        catch (System.Exception ex)
        {
            LogHelper.WriteException(ex, "StartVehicleSex - 进入车辆特殊菜单");
            Notification.PostTicker($"进入车辆互动失败: {ex.Message}", true, false);
        }
    }
}


